/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.rowan.infovis.soccer;

import com.sun.opengl.util.GLUT;
import java.util.ArrayList;
import javax.media.opengl.GL;

public class PlayerMenu {

    private GLUT glut;
    private Alphabet alphabetTeam;
    private Alphabet alphabetPlayer;
    private TeamManager teamManager;

    private float startX;
    private float startY;

    private float LETTER_PAD = 10f;

    private float TEAM_NAME_Y1 = -19.0f;
    private float TEAM_NAME_Y2 = -13.0f;

    private float PLAYER_NAME_Y1 = -40.0f;
    private float PLAYER_NAME_Y2 = -28.0f;

    private ArrayList<String> teamNames;
    private ArrayList<String> playerNames;

    private int numOfTeams;
    private int numOfPlayers;

    //2D array to story startx and stopx of team names
    private float[][] teamNamePos;
    private boolean[] onTeamName;
    private boolean[] teamNameSelected;

    //2D array to story startx and stopx of player names
    private float[][] playerNamePos;
    private boolean[] onPlayerName;
    private boolean[] playerNameSelected;

    private StateManager state;

    public PlayerMenu(TeamManager tm, StateManager s) {
        glut = new GLUT();
        alphabetTeam = new Alphabet();
        alphabetPlayer = new Alphabet();
        this.teamManager = tm;
        this.state = s;
        teamNamePos = new float[50][2];
        onTeamName = new boolean[50];
        teamNameSelected = new boolean[50];
        playerNamePos = new float[50][2];
        onPlayerName = new boolean[50];
        playerNameSelected = new boolean[50];
    }

    public void draw(GL gl, float x, float y) {

        startX = x;
        startY = y;

        drawPlayer(gl);

        if(state.getState()==2) {
            drawSecondPlayer(gl);
        }

        alphabetTeam.draw(gl, startX, startY);

        gl.glPushMatrix();
            gl.glColor3f(1f,1f,1f);
            gl.glRasterPos2f(-65f,-15f);
            glut.glutBitmapString(GLUT.BITMAP_8_BY_13, "Team");
        gl.glPopMatrix();

        drawTeamNames(gl);

        gl.glPushMatrix();
            gl.glColor3f(1f,1f,1f);
            gl.glBegin(GL.GL_LINES);
                gl.glVertex2f(0f,-22f);
                gl.glVertex2f(728f,-22f);
            gl.glEnd();
        gl.glPopMatrix();

        gl.glPushMatrix();
            gl.glColor3f(1f,1f,1f);
            gl.glRasterPos2f(-75f,-37f);
            glut.glutBitmapString(GLUT.BITMAP_8_BY_13, "Player");
        gl.glPopMatrix();

        drawPlayerNames(gl);

        gl.glTranslatef(0f,-62f,0f);

        alphabetPlayer.draw(gl, startX, startY-62f);
    }

    void mouseReleased(int x, int y) {
        alphabetTeam.mouseReleased(x, y);
        alphabetPlayer.mouseReleased(x, y);

        float mouseX = x - startX;
        float mouseY = y - startY;

        for(int i=0; i<numOfTeams; i++) {

            if (onTeamName[i]) {
                teamNameSelected[i] = true;
                if(state.leftSelection()) {
                    teamManager.setTeam(teamNames.get(i));
                } else {
                    teamManager.setSecondTeam(teamNames.get(i));
                }
            }
            else {
                teamNameSelected[i] = false;
            }
        }

        for(int i=0; i<numOfPlayers; i++) {

            if (onPlayerName[i]) {
                playerNameSelected[i] = true;
                if(state.leftSelection()) {
                    teamManager.getCurrentTeam().setCurrentPlayer(playerNames.get(i));
                } else {
                    teamManager.getSecondCurrentTeam().setSecondCurrentPlayer(playerNames.get(i));
                }
            }
            else {
                playerNameSelected[i] = false;
            }
        }

        System.out.println("("+mouseX+","+mouseY+")");
    }

    void mouseMoved(int x, int y) {
        alphabetTeam.mouseMoved(x, y);
        alphabetPlayer.mouseMoved(x, y);

        float mouseX = x - startX;
        float mouseY = y - startY;

        for(int i=0; i<numOfTeams; i++) {

            if (mouseX >= teamNamePos[i][0] && mouseX <= teamNamePos[i][1] &&
                mouseY >= TEAM_NAME_Y1 && mouseY <= TEAM_NAME_Y2)
            {
                onTeamName[i] = true;
            }
            else
            {
                onTeamName[i] = false;
            }
        }

        for(int i=0; i<numOfPlayers; i++) {

            if (mouseX >= playerNamePos[i][0] && mouseX <= playerNamePos[i][1] &&
                mouseY >= PLAYER_NAME_Y1 && mouseY <= PLAYER_NAME_Y2)
            {
                onPlayerName[i] = true;
            }
            else
            {
                onPlayerName[i] = false;
            }
        }
    }

    private void drawPlayer(GL gl) {
        String team = teamManager.getCurrentTeam().getName();
        String fn = teamManager.getCurrentTeam().getCurrentPlayer().getFirstName();
        String ln = teamManager.getCurrentTeam().getCurrentPlayer().getLastName();

        gl.glPushMatrix();
            gl.glColor3f(1f,1f,1f);
            gl.glRasterPos2f(150f,600f);
            glut.glutBitmapString(GLUT.BITMAP_9_BY_15, ln + ", " + fn);
        gl.glPopMatrix();

        gl.glPushMatrix();
            gl.glColor3f(1f,1f,1f);
            gl.glRasterPos2f(150f,575f);
            glut.glutBitmapString(GLUT.BITMAP_HELVETICA_18, team);
        gl.glPopMatrix();
    }

    private void drawSecondPlayer(GL gl) {
        String team = teamManager.getSecondCurrentTeam().getName();
        String fn = teamManager.getSecondCurrentTeam().getSecondCurrentPlayer().getFirstName();
        String ln = teamManager.getSecondCurrentTeam().getSecondCurrentPlayer().getLastName();

        gl.glPushMatrix();
            gl.glColor3f(1f,1f,1f);
            gl.glRasterPos2f(630f,600f);
            glut.glutBitmapString(GLUT.BITMAP_9_BY_15, ln + ", " + fn);
        gl.glPopMatrix();

        gl.glPushMatrix();
            gl.glColor3f(1f,1f,1f);
            gl.glRasterPos2f(630f,575f);
            glut.glutBitmapString(GLUT.BITMAP_HELVETICA_18, team);
        gl.glPopMatrix();
    }

    private void drawTeamNames(GL gl) {
        String letter = alphabetTeam.getSelectedLetter();

        float nameXPos = 5f;

        if(!letter.isEmpty()) {
            teamNames = teamManager.getTeamNames(letter);

            numOfTeams = teamNames.size();

            if(!teamNames.isEmpty()) {
                for(int i=0; i<numOfTeams; i++) {
                    gl.glPushMatrix();
                        setTeamNameColor(gl,i);
                        gl.glRasterPos2f(nameXPos,-15f);
                        glut.glutBitmapString(GLUT.BITMAP_8_BY_13, teamNames.get(i));
                    gl.glPopMatrix();
                    teamNamePos[i][0] = nameXPos;
                    nameXPos += teamNames.get(i).length() * LETTER_PAD;
                    teamNamePos[i][1] = nameXPos;
                }
            }
        }
    }

    private void drawPlayerNames(GL gl) {
        String letter = alphabetPlayer.getSelectedLetter();

        float nameXPos = 5f;

        if(!letter.isEmpty()) {
            if(state.leftSelection()) {
                playerNames = teamManager.getCurrentTeam().getPlayerNames(letter);
            } else {
                playerNames = teamManager.getSecondCurrentTeam().getPlayerNames(letter);
            }

            numOfPlayers = playerNames.size();

            if(!playerNames.isEmpty()) {
                for(int i=0; i<numOfPlayers; i++) {
                    gl.glPushMatrix();
                        setPlayerNameColor(gl,i);
                        gl.glRasterPos2f(nameXPos,-37f);
                        glut.glutBitmapString(GLUT.BITMAP_8_BY_13, playerNames.get(i));
                    gl.glPopMatrix();
                    playerNamePos[i][0] = nameXPos;
                    nameXPos += playerNames.get(i).length() * LETTER_PAD;
                    playerNamePos[i][1] = nameXPos;
                }
            }
        }
    }

    private void setTeamNameColor(GL gl, int i) {
        if (teamNameSelected[i]) {
            gl.glColor3f(0f,0f,1f);
        }
        else if(onTeamName[i]) {
            gl.glColor3f(1f,0f,0f);
        }
        else {
            gl.glColor3f(1f,1f,1f);
        }
    }

    private void setPlayerNameColor(GL gl, int i) {
        if (playerNameSelected[i]) {
            gl.glColor3f(0f,0f,1f);
        }
        else if(onPlayerName[i]) {
            gl.glColor3f(1f,0f,0f);
        }
        else {
            gl.glColor3f(1f,1f,1f);
        }
    }
}